package it.eng.area118.mdocommon.android.geolocation;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import android.location.Location;

import it.eng.area118.mdocommon.geolocation.Geolocation;
import it.eng.area118.mdocommon.geolocation.GeolocationCallback;
import it.eng.area118.mdocommon.geolocation.GpsLocation;
import it.eng.area118.mdocommon.geolocation.GpsStatusListener;

public class EmulatorGeoLocationService implements Geolocation {

	protected List<GpsStatusListener> gpsStatusListeners = new ArrayList<GpsStatusListener>();

	protected List<GeolocationCallback> watchers = new ArrayList<GeolocationCallback>();

	@Override
	public boolean start() {

		new Thread(new Runnable() {

			public void run() {
				String emulated = "11.31587981738889,44.50404954890708,$11.31582257002616,44.50394975299302,$11.3157767714106,44.50386991416819,$11.31571952067243,44.50377011139049,$11.31564582083166,44.50369838377758,$11.31560001776648,44.50361853662353,$11.31555421195999,44.503538686983,$11.31549701986857,44.50343885487614,$11.3155469103965,44.50335445471415,$11.31568682400975,44.50331372545543,$11.31577077430869,44.50328928842926,$11.31599470112655,44.50322410546733,$11.31601133757138,44.50319597145351,$11.31626335998757,44.50312261541941,$11.31637536899616,44.50309001324255,$11.3165993839393,44.50302480783476,$11.31665538773559,44.50300850764937,$11.31682864500539,44.50291149546432,$11.31694064835722,44.50287889344955,$11.31702989262773,44.50280633692042,$11.31714189028326,44.50277373726377,$11.31724250708032,44.50272116074924,$11.31738250103543,44.50268041212815,$11.31753910791525,44.50261153823324,$11.31766783492914,44.50255079875417,$11.31779658754633,44.50249005915436,$11.31795363368953,44.50242113925945,$11.31809403780611,44.50238035168937,$11.31823439574922,44.50233957647263,$11.31843082124047,44.50228251180592,$11.31853169756011,44.50222996131561,$11.31867190712131,44.50218923271581,$11.31878404245057,44.50215665869211,$11.31892415549202,44.50211595269548,$11.31899678263889,44.50207159957332,$11.31912534112903,44.50201098193435,$11.31929315895886,44.5019621706563,$11.31940502391986,44.50192963192971,$11.31953350735145,44.50186904359713,$11.31968991637264,44.50180033795309,$11.31980173067606,44.50176781725037,$11.31996943026064,44.50171904169121,$11.32010916199261,44.50167840089544,$11.32024887609997,44.50163776509633,$11.32038857335615,44.50159713228174,$11.32055618787599,44.50154838139284,$11.32065653360148,44.50149599328087,$11.32079617371574,44.50145537883417,$11.32093579639206,44.50141476955191,$11.3210919239737,44.50134612635151,$11.32123154041067,44.50130544160601,$11.32137117221935,44.50126475237963,$11.32147144371431,44.50121235786595,$11.32159958819198,44.50115189643098,$11.32175560578327,44.50108333819156,$11.32186720101692,44.50105090184171,$11.32197877995821,44.50101847168938,$11.32209034177603,44.50098604699949,$11.32221835800725,44.50092563637127,$11.32235777091031,44.50088511140564,$11.32252506049399,44.50083646988635,$11.32258081753958,44.50082025813964,$11.32276450242092,44.50074366321652,$11.32293171653919,44.50069504443339,$11.32308745596939,44.50062658186681,$11.32322675177807,44.50058608134053,$11.3233381747978,44.50055368536254,$11.32346598057219,44.50049335140677,$11.32357740629645,44.50046094461531,$11.32357740629645,44.50046094461531,$11.32357740629645,44.50046094461531,$11.32357740629645,44.50046094461531,$11.32357740629645,44.50046094461531,$11.32357740629645,44.50046094461531,$11.32357740629645,44.50046094461531,$11.32357740629645,44.50046094461531,$11.32357740629645,44.50046094461531,$11.32357740629645,44.50046094461531,$11.32357740629645,44.50046094461531,$11.32357740629645,44.50046094461531,$11.32357740629645,44.50046094461531,$11.32357740629645,44.50046094461531,$11.32357740629645,44.50046094461531,$11.32357740629645,44.50046094461531,$11.32357740629645,44.50046094461531,$11.32357740629645,44.50046094461531,$11.32357740629645,44.50046094461531,$11.32357740629645,44.50046094461531,$11.32357740629645,44.50046094461531,$11.32357740629645,44.50046094461531,$11.32357740629645,44.50046094461531,$11.32357740629645,44.50046094461531,$11.32357740629645,44.50046094461531,$11.32357740629645,44.50046094461531,$11.32357740629645,44.50046094461531,$11.32357740629645,44.50046094461531,$11.32357740629645,44.50046094461531,$11.32357740629645,44.50046094461531,$11.32357740629645,44.50046094461531,$11.32357740629645,44.50046094461531,$11.32357740629645,44.50046094461531,$11.32357740629645,44.50046094461531,$11.32357740629645,44.50046094461531,$11.32357740629645,44.50046094461531,$11.32357740629645,44.50046094461531,$11.32357740629645,44.50046094461531,$11.32357740629645,44.50046094461531,$11.32357740629645,44.50046094461531,$11.32357740629645,44.50046094461531,$11.32357740629645,44.50046094461531,$11.32357740629645,44.50046094461531,$11.32357740629645,44.50046094461531,$11.32357740629645,44.50046094461531,$11.32357740629645,44.50046094461531,$11.32357740629645,44.50046094461531,$11.32357740629645,44.50046094461531,$11.32357740629645,44.50046094461531,$11.32357740629645,44.50046094461531,$11.32357740629645,44.50046094461531,$11.32357740629645,44.50046094461531,$11.32357740629645,44.50046094461531,$11.32357740629645,44.50046094461531,$11.32357740629645,44.50046094461531,$11.32357740629645,44.50046094461531,$11.32357740629645,44.50046094461531,$11.32357740629645,44.50046094461531,$11.32357740629645,44.50046094461531,$11.32357740629645,44.50046094461531,$11.32357740629645,44.50046094461531,$11.32357740629645,44.50046094461531,$11.32357740629645,44.50046094461531,$11.32357740629645,44.50046094461531,$11.32357740629645,44.50046094461531,$11.32357740629645,44.50046094461531,$11.32357740629645,44.50046094461531,$11.32357740629645,44.50046094461531,$11.32357740629645,44.50046094461531,$11.32357740629645,44.50046094461531,$11.32357740629645,44.50046094461531,$11.32357740629645,44.50046094461531,$11.32357740629645,44.50046094461531,$11.32357740629645,44.50046094461531,$11.32357740629645,44.50046094461531,$11.32357740629645,44.50046094461531,$11.32357740629645,44.50046094461531,$11.32357740629645,44.50046094461531,$11.32357740629645,44.50046094461531,$11.32357740629645,44.50046094461531,$11.32357740629645,44.50046094461531,$11.32357740629645,44.50046094461531,$11.32357740629645,44.50046094461531,$11.32357740629645,44.50046094461531,$11.32357740629645,44.50046094461531,$11.32371687901483,44.50042031718836,$11.32388425394862,44.50037156319915,$11.32398435982133,44.50031922688238,$11.32409594692333,44.5002867220387,$11.32426333462117,44.50023796406523,$11.32443073010137,44.50018920221311,$11.32451935855697,44.50011702521024,$11.32463113147147,44.50008442167776,$11.32471497062297,44.50005996576917,$11.32482675596056,44.500027361734,$11.32499409326129,44.49997863492004,$11.32510565149904,44.49994615180993,$11.32527298851842,44.49989742576092,$11.32540731803583,44.49990472714536,$11.32549106474214,44.49988032545731,$11.3256865193564,44.49982337046103,$11.32583752931284,44.49980257757209,$11.32592130715408,44.4997781649185,$11.32606094483164,44.499737474802,$11.32620059160221,44.49969678179956,$11.32634024755159,44.49965608538185,$11.32651815385786,44.49951137850511,$11.32664644177599,44.49945077534581,$11.32673025341498,44.49942634977669,$11.32691431956929,44.49934950777394,$11.32700271042917,44.4992774069857,$11.32703556701842,44.49922143788798,$11.32714036629695,44.49912137284958,$11.32726780796711,44.49906116753501,$11.32744564625255,44.49903274582103,$11.32758001858671,44.49904001883598,$11.32774699506325,44.49899144986161,$11.32786983321364,44.49897889645876,$11.32792548481255,44.49896270787074,$11.32812025101156,44.49890605193811,$11.32823153561152,44.49887367982081,$11.32835435218209,44.49886113088768,$11.32852125539268,44.49881257786463,$11.32866009522869,44.4987722224225,$11.32874326109932,44.49874806463016,$11.3289372774843,44.49869170874165,$11.32899270029366,44.49867560994608,$11.32918596371447,44.49861955810134,$11.32931258776937,44.49855966063488,$11.32947960820083,44.49851103877205,$11.32957977562023,44.49845866810985,$11.32967984444892,44.49840633919368,$11.32973551840096,44.49839013010586,$11.32991884117974,44.49831359667496,$11.33003018148394,44.49828118239034,$11.3301578176191,44.49822086108422,$11.33025761491387,44.49816864535259,$11.33042460623044,44.49812002850833,$11.33056376120722,44.49807951614903,$11.33070291249334,44.49803900449187,$11.3308420595876,44.49799849421973,$11.33103652119576,44.49794192017968,$11.33117535600281,44.49790153414453,$11.33133278087635,44.49794834395129,$11.33143469034179,44.49801131604107,$11.33156438095896,44.49806621774815,$11.33170568906885,44.49814091318272,$11.33182380733922,44.49817603757121,$11.33194193502691,44.49821116492987,$11.33295808905839,44.49872680434709,$11.33324210163576,44.49871365062586,$11.33332528741513,44.498689413357,$11.33346391735198,44.4986490216815,$11.33365797198144,44.49859247974364,$11.33376884549356,44.49856017280703,$11.33397432671987,44.49852349286831,$11.33413959110475,44.4984753168274,$11.33422209062422,44.49845126560871,$11.33439905166345,44.49842287318312,$11.33456677998156,44.49837402946221,$11.33470626279514,44.49833340135807,$11.33484517335735,44.49829293155729,$11.3349794173981,44.49830019101694,$11.33513678715356,44.49834708752442,$11.33525653463016,44.49838182405858,$11.33540466884117,44.49840832471733,$11.3354970209452,44.49845109883194,$11.33569848850949,44.49850864154212,$11.33595172558392,44.49855095410324,$11.33604252236387,44.49859416955651,$11.33614430881444,44.49865736667144,$11.33628467554814,44.49873248910588,$11.33644020171614,44.49877988169803,$11.33654496398031,44.49879563408011,$11.3367392940572,44.49885485285677,$11.33687098755327,44.49886265115556,$11.33704148082754,44.49888228632503,$11.33716986426452,44.49893757349494,$11.33727455793638,44.49895328482944,$11.33744531660144,44.49897282043263,$11.33758898800816,44.49900031289328,$11.33772078268347,44.49900801969856,$11.33790638390014,44.49899983270522,$11.33806333449183,44.49899988937563,$11.33815527470271,44.49899593901449,$11.33825952983146,44.49901166116638,$11.33840238376892,44.49903921704075,$11.33847226512716,44.49915799767988,$11.3384918859845,44.49924516691418,$11.3385384880632,44.4993243810629,$11.33859674984913,44.49942341447765,$11.33857832889761,44.49949872626733,$11.33861031552599,44.49960569438532,$11.33869244852572,44.49974436782907,$11.33876286736895,44.49986326201228,$11.33883118902836,44.49998243100544,$11.33886067546749,44.50008980899342,$11.33888139282904,44.50017705400461,$11.33894047775871,44.50032353433812,$11.33897502483145,44.50043053057426,$11.33899729773455,44.50051778286876,$11.3390316542043,44.50062491503686,$11.33910526712684,44.50074388585699,$11.33912981596055,44.50078355800243,$11.33921576933664,44.50092247257952,$11.33928949108463,44.50104161613697,$11.33936325620402,44.50116083015976,$11.33946167550375,44.50131988911616,$11.33951960401959,44.50141948634726,$11.33956537243391,44.50149919082124,$11.3396328598868,44.50161877457974,$11.33967773073953,44.50169849276784,$11.31582257002616,44.50394975299302,$11.3157767714106,44.50386991416819,$11.31571952067243,44.50377011139049,$11.31564582083166,44.50369838377758,$11.31560001776648,44.50361853662353,$11.31555421195999,44.503538686983,$11.31549701986857,44.50343885487614,$11.3155469103965,44.50335445471415,$11.31568682400975,44.50331372545543,$11.31577077430869,44.50328928842926,$11.31599470112655,44.50322410546733,$11.31601133757138,44.50319597145351,$11.31626335998757,44.50312261541941,$11.31637536899616,44.50309001324255,$11.3165993839393,44.50302480783476,$11.31665538773559,44.50300850764937,$11.31682864500539,44.50291149546432,$11.31694064835722,44.50287889344955,$11.31702989262773,44.50280633692042,$11.31714189028326,44.50277373726377,$11.31724250708032,44.50272116074924,$11.31738250103543,44.50268041212815,$11.31753910791525,44.50261153823324,$11.31766783492914,44.50255079875417,$11.31779658754633,44.50249005915436,$11.31795363368953,44.50242113925945,$11.31809403780611,44.50238035168937,$11.31823439574922,44.50233957647263,$11.31843082124047,44.50228251180592,$11.31853169756011,44.50222996131561,$11.31867190712131,44.50218923271581,$11.31878404245057,44.50215665869211,$11.31892415549202,44.50211595269548,$11.31899678263889,44.50207159957332,$11.31912534112903,44.50201098193435,$11.31929315895886,44.5019621706563,$11.31940502391986,44.50192963192971,$11.31953350735145,44.50186904359713,$11.31968991637264,44.50180033795309,$11.31980173067606,44.50176781725037,$11.31996943026064,44.50171904169121,$11.31582257002616,44.50394975299302,$11.3157767714106,44.50386991416819,$11.31571952067243,44.50377011139049,$11.31564582083166,44.50369838377758,$11.31560001776648,44.50361853662353,$11.31555421195999,44.503538686983,$11.31549701986857,44.50343885487614,$11.3155469103965,44.50335445471415,$11.31568682400975,44.50331372545543,$11.31577077430869,44.50328928842926,$11.31599470112655,44.50322410546733,$11.31601133757138,44.50319597145351,$11.31626335998757,44.50312261541941,$11.31637536899616,44.50309001324255,$11.3165993839393,44.50302480783476,$11.31665538773559,44.50300850764937,$11.31682864500539,44.50291149546432,$11.31694064835722,44.50287889344955,$11.31702989262773,44.50280633692042,$11.31714189028326,44.50277373726377,$11.31724250708032,44.50272116074924,$11.31738250103543,44.50268041212815,$11.31753910791525,44.50261153823324,$11.31766783492914,44.50255079875417,$11.31779658754633,44.50249005915436,$11.31795363368953,44.50242113925945,$11.31809403780611,44.50238035168937,$11.31823439574922,44.50233957647263,$11.31843082124047,44.50228251180592,$011.29332,44.72851,$11.31853169756011,44.50222996131561,$11.31867190712131,44.50218923271581,$11.31878404245057,44.50215665869211,$11.31892415549202,44.50211595269548,$11.31899678263889,44.50207159957332,$11.31912534112903,44.50201098193435,$11.31929315895886,44.5019621706563,$11.31940502391986,44.50192963192971,$11.31953350735145,44.50186904359713,$11.31968991637264,44.50180033795309,$11.31980173067606,44.50176781725037,$11.31996943026064,44.50171904169121";
				String[] coords = emulated.split("\\$");
				for (String cd : coords) {
					try {
						Random generator = new Random();
						String lat = cd.split("\\,")[1];
						String lon = cd.split("\\,")[0];
						GpsLocation loc = new GpsLocation();
						loc.setLatitude(Double.valueOf(lat));
						loc.setLongitude(Double.valueOf(lon));
						if (lon.equals("11.32357740629645")) // SIMULO L'ARRIVO
																// SUL LUOGO
						{
							loc.setSpeed(generator.nextInt(5));
						} else {
							loc.setSpeed(generator.nextInt(50));
						}

						notifyWatchers(loc);

						Thread.sleep(800);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}

			}
		}).start();

		return false;
	}

	@Override
	public boolean stop() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public GpsLocation getLocation() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void watchForLocation(GeolocationCallback callback) {
		this.watchers.add(callback);

	}

	@Override
	public void addGpsStatusListener(GpsStatusListener listener) {
		// TODO Auto-generated method stub

	}

	@Override
	public boolean removeWatcher(GeolocationCallback callback) {
		// TODO Auto-generated method stub
		return false;
	}

	private void notifyWatchers(GpsLocation location) {
		synchronized (watchers) {
			for (GeolocationCallback callback : watchers) {
				GpsLocation gpsLocation = new GpsLocation();
				callback.onLocationReceived(location);
			}
		}

	}

	@Override
	public boolean isActive() {
		return true;
	}
}